package com.campus.common.service.impl;

import com.campus.common.domain.Product;
import com.campus.common.mapper.ProductMapper;
import com.campus.common.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @program: campus
 * @description:
 * @author: ENDING
 * @create: 2024-04-21 21:00
 **/

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public String test() {

        return productMapper.test().toString();
    }

    @Override
    public boolean save(Product product) {

        if(product.getProductId() == null) {
            return this.insertProduct(product);
        }
        return this.updateProduct(product);
    }

    @Override
    public boolean insertProduct(Product product) {
        product.setGmtCreate(LocalDateTime.now());
        product.setGmtModified(LocalDateTime.now());
        int affect = productMapper.insertProduct(product);
        return affect != 0;
    }

    @Override
    public boolean updateProduct(Product product) {
        product.setGmtModified(LocalDateTime.now());
        int affect = productMapper.updateProduct(product);
        return affect != 0;
    }


}
